package com.mall.cloud.controller;

import com.mall.cloud.bean.User;
import com.mall.cloud.common.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/user", produces = { "application/json;charset=UTF-8" })
public class UserController {

    private Logger LOGGER = LoggerFactory.getLogger(this.getClass());

    @PostMapping("/create")
    public CommonResult<User> create(@RequestBody User user) {
        LOGGER.info("创建用户，用户信息为：{}",user.toString());
        return new CommonResult<>("操作成功", 200);
    }

    @GetMapping("/{id}")
    public CommonResult<User> getUser(@PathVariable Long id) {
        LOGGER.info("根据id获取用户信息，用户名称为：{}","张三");
        return new CommonResult<>(new User(id,"张三"));
    }

    @GetMapping("/getUserByIds")
    public CommonResult<List<User>> getUserByIds(@RequestParam List<Long> ids) {
        List<User> userList= new ArrayList<>();
        for(Long id : ids){
            userList.add(new User(id, "张三"));
        }
        LOGGER.info("根据ids获取用户信息，用户列表为：{}",userList);
        return new CommonResult<>(userList);
    }

    @GetMapping("/getByUsername")
    public CommonResult<User> getByUsername(@RequestParam String username) {
        LOGGER.info("根据username获取用户信息，用户列表为：{}",username);
        return new CommonResult<>(new User(1L, "张三"));
    }

    @PutMapping("/update")
    public CommonResult<User> update(@RequestBody User user) {
        LOGGER.info("更新用户信息，用户为：{}",user);
        return new CommonResult<>("操作成功", 200);
    }

    @DeleteMapping("/delete/{id}")
    public CommonResult<User> delete(@PathVariable Long id) {
        LOGGER.info("删除用户信息，用户id为：{}",id);
        return new CommonResult<>("操作成功", 200);
    }
}
